/**
 * ZHU framework. Copyright 2012-, all rights reserved.
 *
 * $Id: ConversationalController.java 39 2012-09-12 08:50:12Z zhuxiaopeng $
 * $Revision: 39 $
 * $Date: 2012-09-12 16:50:12 +0800 (三, 2012-09-12) $
 * $Author: zhuxiaopeng $
 */
package zhu.framework.web.application;

import java.util.*;

import javax.faces.*;
import javax.faces.application.*;
import javax.faces.context.*;
import javax.faces.event.*;

public class WebExceptionHandler extends ExceptionHandlerWrapper {

	private final ExceptionHandler wrapped;

	public WebExceptionHandler(final ExceptionHandler wrapped) {
		this.wrapped = wrapped;
	}

	@Override
	public ExceptionHandler getWrapped() {
		return this.wrapped;
	}

	@Override
	public void handle() throws FacesException {
		final Iterable<ExceptionQueuedEvent> events = this.wrapped.getUnhandledExceptionQueuedEvents();
		for (final Iterator<ExceptionQueuedEvent> it = events.iterator(); it.hasNext();) {
			final ExceptionQueuedEvent event = it.next();
			final ExceptionQueuedEventContext eqec = event.getContext();

			if (eqec.getException() instanceof ViewExpiredException) {
				final FacesContext context = eqec.getContext();
				final NavigationHandler navHandler = context.getApplication().getNavigationHandler();
				navHandler.handleNavigation(context, null, "/session_timeout?faces-redirect=true&expired=true");
			}
			it.remove();
		}
		this.wrapped.handle();
	}
}
